from pynput import keyboard
import time

count = 0
start_time = None
duration = 1.0  # 监听时长，单位秒

def on_press(key):
    global count, start_time
    try:
        if key.char == 'j':
            if start_time is None:
                start_time = time.time()
            current_time = time.time()
            if current_time - start_time <= duration:
                count += 1
            else:
                # 时间到，停止监听
                return False
    except AttributeError:
        # 处理特殊按键
        pass

def main():
    global count, start_time
    print(f"请在 {duration} 秒内尽可能多按 'J' 键...")
    with keyboard.Listener(on_press=on_press) as listener:
        listener.join()
    print(f"你在 {duration} 秒内按了 {count} 次 J 键。")
    print(f"平均每秒按键次数: {count / duration:.2f} 次")

if __name__ == "__main__":
    main()
